package com.rogue.system.controller;

import com.github.pagehelper.PageInfo;
import com.rogue.common.core.utils.ResultDTO;
import com.rogue.system.domain.dto.UserDTO;
import com.rogue.system.domain.dto.UserPageDTO;
import com.rogue.system.domain.entity.SysUser;
import com.rogue.system.domain.vo.UserRoleVo;
import com.rogue.system.domain.vo.UserVo;
import com.rogue.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author YeahLiang
 * @Create 2022/03/16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
// @Api(value = "user", tags = "用户管理模块")
public class UserController {

    private final SysUserService userService;

    @RequestMapping("/easyCreateUser")
    public ResultDTO<Boolean> easyCreateUser(@RequestBody UserDTO userDTO) {
        return ResultDTO.success(userService.save(userDTO));
    }

    @RequestMapping("/easyDeleteUser/{id}")
    public ResultDTO<Boolean> easyDeleteUser(@PathVariable Long id) {
        return ResultDTO.success(userService.removeById(id));
    }

    @RequestMapping("/easyUpdateUser")
    public ResultDTO<Boolean> easyUpdateUser(@RequestBody UserDTO userDTO) {
        return ResultDTO.success(userService.updateById(userDTO));
    }

//    @PreAuthorize("hasAuthority('system:user:query')")
    @GetMapping("/easyGetOneUser/{id}")
    public ResultDTO<SysUser> easyGetOneUser(@PathVariable Long id) {
        return ResultDTO.success(userService.getById(id));
    }

    /**
     * @description: 根据用户 id 锁定用户
     * @author YeahLiang
     * @date: 2022/03/21 11:43
     * @param id 用户 id
     * @return com.rogue.common.core.utils.ResultDTO<java.lang.Boolean>
     */
    @GetMapping("/lockUser/{id}")
    public ResultDTO<Boolean> lockUser(@PathVariable Long id){
        return ResultDTO.success(userService.lockUserById(id));
    }

    /**
     * @description: 根据用户 id 解锁用户
     * @author YeahLiang
     * @date: 2022/03/21 11:43
     * @param id 用户 id
     * @return com.rogue.common.core.utils.ResultDTO<java.lang.Boolean>
     */
    @GetMapping("/unlockUser/{id}")
    public ResultDTO<Boolean> unlockUser(@PathVariable Long id){
        return ResultDTO.success(userService.unlockUserById(id));
    }

    /**
     * @description: 恢复用户
     * @author YeahLiang
     * @date: 2022/03/21 12:00
     * @param id
     * @return com.rogue.common.core.utils.ResultDTO<java.lang.Boolean>
     */
    @GetMapping("/recoverUser/{id}")
    public ResultDTO<Boolean> recoverUser(@PathVariable Long id){
        return ResultDTO.success(userService.recoverUser(id));
    }

    /**
     * @description: 创建用户，并设置用户角色权限
     * @author YeahLiang
     * @date: 2022/03/21 12:02
     * @param userDTO
     * @return com.rogue.common.core.utils.ResultDTO<java.lang.Boolean>
     */
    @PostMapping("/createUser")
    public ResultDTO<Boolean> createUser(@RequestBody UserDTO userDTO){
        return ResultDTO.success(userService.createUserAndRole(userDTO));
    }

    /**
     * @description: 删除用户和其角色权限
     * @author YeahLiang
     * @date: 2022/03/21 12:02
     * @param id
     * @return com.rogue.common.core.utils.ResultDTO<java.lang.Boolean>
     */
    @GetMapping("/removeUser/{id}")
    public ResultDTO<Boolean> removeUser(@PathVariable Long id){
        return ResultDTO.success(userService.removeUserAndRole(id));
    }

    /**
     * 条件分页查询
     * @author YeahLiang
     * @date: 2022/03/24 16:21
     * @param userPageDTO
     * @return com.rogue.common.core.utils.ResultDTO<com.github.pagehelper.PageInfo<com.rogue.system.domain.vo.UserVo>>
     */
    @PostMapping("/listPageUser")
    public ResultDTO<PageInfo<UserVo>> listPageUser(@RequestBody UserPageDTO userPageDTO){
        return ResultDTO.success(userService.listPageUser(userPageDTO));
    }

    @GetMapping("/getUserAndRole/{id}")
    public ResultDTO<UserRoleVo> getUserAndRole(@PathVariable Long id){
        return ResultDTO.success(userService.getUserAndRole(id));
    }

    @GetMapping("/getUserAndRoleByName")
    public ResultDTO<UserRoleVo> getUserAndRoleByName(String name){
        return ResultDTO.success(userService.getUserAndRoleByName(name));
    }
}
